package com.dq.service.messsge.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.InstructionMessageQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.message.oneself.Message;
import com.dq.domain.message.oneself.centre.InstructionStaffuserCentre;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.messsge.InstructionStaffuserCentreService;
import com.dq.service.sys.user.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author cf
 *
 * 岗位逻辑
 */
@Service
public class InstructionStaffuserCentreServiceImpl extends BaseRepositories implements InstructionStaffuserCentreService {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取分配给 员工 的指令列表
     */
    @Override
    public Vo getDispose(@ApiParam("caseUserId: 关联用户ID") InstructionMessageQuery instructionMessageQuery) throws AdminException {
        instructionMessageQuery.format();
        Map<String, Object> query = instructionMessageQuery.parseMap();
        if(instructionMessageQuery.getStaffUserId() != null) {
            query.put("staffUser_id", instructionMessageQuery.getStaffUserId());
            query.remove("staffUserId");
        }
        if(instructionMessageQuery.getOrgId() != null) {
            query.put("org_id", instructionMessageQuery.getOrgId());
            query.remove("orgId");
        }

        List<InstructionStaffuserCentre> list = dao.find(dao.query2Hql(InstructionStaffuserCentre.class, query), query);
        Long count = dao.count(dao.count2Hql(InstructionStaffuserCentre.class, query), query);
        PageHelper.startPage(instructionMessageQuery.getPage(), instructionMessageQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<InstructionStaffuserCentre.Vo>) VO.toVOCollection(list));
        pageInfo.setTotal(count);

        return new Vo(pageInfo);
    }

    /**
     * 添加
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public InstructionStaffuserCentre save(InstructionStaffuserCentre instructionStaffuserCentre)  throws AdminException {
        if(instructionStaffuserCentre.getOrg() == null) {
            throw new AdminException("未关联门店");
        }
        if(instructionStaffuserCentre.getStaffUser() == null) {
            throw new AdminException("未关联用户");
        }
        if(instructionStaffuserCentre.getInstructionMessage() == null) {
            throw new AdminException("未关联指令消息");
        }

        instructionStaffuserCentre.setVersion(Constant.VERSION_VALUE);
        instructionStaffuserCentre.setDeleted(false);
        instructionStaffuserCentre.setCreateTime(System.currentTimeMillis());
        instructionStaffuserCentre.setLastModifyTime(System.currentTimeMillis());

        if(instructionStaffuserCentre.getMessageStatus() == null) {
            instructionStaffuserCentre.setMessageStatus(Message.MessageStatus.noSee);
        }

        SysUser user = sysUserService.getUser();
        instructionStaffuserCentre.setCreateUser(user);
        instructionStaffuserCentre.setUpdateUser(user);

        // 门店信息， 用来搜索一个门店的所有指令
        instructionStaffuserCentre.setOrg(instructionStaffuserCentre.getStaffUser().getSection());

        return instructionStaffuserCentreRepository.save(instructionStaffuserCentre);
    }

    /**
     *修改
     */
    @Override
    public InstructionStaffuserCentre update(InstructionStaffuserCentre instructionStaffuserCentre) throws AdminException {
        instructionStaffuserCentre.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        if(user != null) {
            instructionStaffuserCentre.setUpdateUser(user);
        }

        return instructionStaffuserCentreRepository.save(instructionStaffuserCentre);
    }

}

